﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Gmantis.Controls.DataGrid
{
    public class DataGridGroupRow : DataGridRow
    {
        // Fields
        private Visibility _groupRowsVisibility;

        // Events
        public event EventHandler<PropertyChangedEventArgs<Visibility>> GroupRowsVisibilityChanged;

        // Methods
        protected internal override void BindRowPresenter(DataGridRowPresenter presenter)
        {
            DataGridGroupRowPresenter presenter2 = presenter as DataGridGroupRowPresenter;
            if (this.Column == null)
            {
                TextBlock block = new TextBlock
                {
                    Text = (base.DataItem == null) ? "" : base.DataItem.ToString()
                };
                presenter2.GroupContent = block;
            }
            else
            {
                presenter2.GroupContent = this.Column.CreateGroupContent();
            }
            Binding binding = new Binding
            {
                Mode = BindingMode.TwoWay,
                Converter = new VisibilityConverter()
            };
            presenter2.SetBinding(DataGridGroupRowPresenter.IsCheckedProperty, binding.From<DataGridGroupRow>(this, x => x.GroupRowsVisibility));
            presenter.DataContext = base.DataItem;
        }

        protected internal override DataGridRowPresenter CreateRowPresenter()
        {
            DataGridGroupRowPresenter target = new DataGridGroupRowPresenter();
            target.SetBinding<DataGrid>(DataGridGroupRowPresenter.ButtonBackgroundProperty, base.DataGrid, x => x.HeaderBackground);
            target.SetBinding<DataGrid>(DataGridGroupRowPresenter.ButtonForegroundProperty, base.DataGrid, x => x.HeaderForeground);
            return target;
        }

        public string GetGroupText()
        {
            return this.Column.GetGroupText(this);
        }

        protected internal override object GetRowPresenterRecyclingKey()
        {
            return typeof(DataGridGroupRowPresenter);
        }

        protected internal override bool HasCellPresenter(DataGridColumn column)
        {
            return false;
        }

        protected internal override void OnLoaded()
        {
            base.OnLoaded();
            base.IsEditable = false;
            base.ShowVerticalFreezingSeparator = false;
            base.RowStyle = base.DataGrid.GroupRowStyle;
            base.RowHeaderStyle = base.DataGrid.GroupRowHeaderStyle;
        }

        protected internal override void UnbindRowPresenter(DataGridRowPresenter presenter)
        {
            DataGridGroupRowPresenter presenter2 = presenter as DataGridGroupRowPresenter;
            presenter2.ClearValue(DataGridGroupRowPresenter.IsCheckedProperty);
            presenter2.ClearValue(DataGridGroupRowPresenter.GroupContentProperty);
            presenter.DataContext = null;
        }

        // Properties
        public DataGridColumn Column { get; internal set; }

        protected override DataGridLength DefaultHeight
        {
            get
            {
                if (base.DataGrid == null)
                {
                    return new DataGridLength(25.0);
                }
                return base.DataGrid.GroupRowHeight;
            }
        }

        public object[] GroupKeys { get; internal set; }

        public Visibility GroupRowsVisibility
        {
            get
            {
                return this._groupRowsVisibility;
            }
            set
            {
                Visibility visibility = this._groupRowsVisibility;
                if (visibility != value)
                {
                    this._groupRowsVisibility = value;
                    if (this.GroupRowsVisibilityChanged != null)
                    {
                        PropertyChangedEventArgs<Visibility> e = new PropertyChangedEventArgs<Visibility>
                        {
                            OldValue = visibility,
                            NewValue = value
                        };
                        this.GroupRowsVisibilityChanged(this, e);
                    }
                    base.OnPropertyChanged("GroupRowsVisibility");
                }
            }
        }

        public override int Level
        {
            get
            {
                if (base.ParentGroup != null)
                {
                    return base.ParentGroup.GroupKeys.Length;
                }
                return 0;
            }
        }

        public DataGridRowCollection Rows { get; internal set; }
    }
}
